﻿Imports System.Data.OleDb

Public Class _like
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sql As String = "SELECT * FROM UserLikes WHERE Game_fkID=@f1 AND UserName=@f2"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("WarriorConnection").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@f2", User.Identity.Name)

        conn.Open()
        Dim dr As OleDbDataReader = cmd.ExecuteReader()

        If dr.HasRows Then
            img_Like.Visible = True
            btn_Like.Visible = False
        Else
            img_Like.Visible = False
            btn_Like.Visible = True
        End If

        conn.Close()

    End Sub

    Protected Sub btn_Save_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Save.Click
        Dim sql As String = "INSERT INTO UserRatingGame(Rating,Rater,Game_fkID) VALUES (@f1,@f2,@f3)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("WarriorConnection").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        Dim sqlDelete As String = "DELETE FROM UserRatingGame WHERE Rater=@f1 AND Game_fkID=@f2"
        Dim cmdDelete As OleDbCommand = New OleDbCommand(sqlDelete, conn)

        cmd.Parameters.AddWithValue("@f1", Rating1.CurrentRating)
        cmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f3", Request.Params("ID"))
        cmdDelete.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmdDelete.Parameters.AddWithValue("@f3", Request.Params("ID"))

        conn.Open()
        cmdDelete.ExecuteNonQuery()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        cmdDelete.Dispose()
        conn.Dispose()
    End Sub

    Protected Sub btn_Like_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Like.Click
        img_Like.Visible = True
        btn_Like.Visible = False

        Dim sql As String = "INSERT INTO UserLikes(Game_fkID,UserName) VALUES (@f1,@f2)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("WarriorConnection").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@f2", User.Identity.Name)

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
    End Sub
End Class